<?php

namespace App\Services\Strator;

use App\Models\Strator\StratorPermission;
use App\Models\Strator\StratorPosition;
use Illuminate\Support\Collection;
use App\Models\Strator\StratorHasPermission;
use App\Models\Strator\StratorHasPosition;
use App\Models\Strator\StratorPositionHasPermission;

class StratorPermissionService
{

    //
    public function getDirectPermissions($strator,$status=0): array{
        $permission_ids=$this->getDirectPermissionIds($strator,$status);
        return $this->getPermissionTreeByPermissinIds($permission_ids,$status);
    }
    //
    public function getDirectPermissionIds($strator,$status=0): Collection{
        if($strator->is_super==1){
            if($status==1){
                return StratorPermission::where('status',1)->pluck('id');
            }
            return StratorPermission::pluck('id');
        }
        return StratorHasPermission::where('strator_id',$strator->id)->pluck('permission_id');
    }

    //
    public function getPositionPermissions($strator,$status): array{
        $permission_ids=$this->getPositionPermissionIds($strator,$status);
        return $this->getPermissionTreeByPermissinIds($permission_ids,$status);
    }
    //
    public function getPositionPermissionIds($strator,$status): Collection{
        if($strator->is_super==1){
            if($status==1){
                return StratorPermission::where('status',1)->pluck('id');
            }
            return StratorPermission::pluck('id');
        }
        $position_ids = StratorHasPosition::where('strator_id',$strator->id)->pluck('position_id');
        return StratorPositionHasPermission::whereIn('position_id',$position_ids)->pluck('permission_id');
    }

    //
    public function getAllPermissions($strator,$status=0): array{
        $permission_ids=$this->getAllPermissionIds($strator,$status);
        return $this->getPermissionTreeByPermissinIds($permission_ids,$status);
    }
    //
    public function getAllPermissionIds($strator,$status=0): Collection{
        $permission_ids = $this->getDirectPermissionIds($strator,$status);
        return $permission_ids->merge($this->getPositionPermissionIds($strator,$status));
    }
    //
    public function getSuperPermissionIds(): Collection{
        return StratorPermission::pluck('id');
    }

    //
    public function getPermissionTreeByPermissinIds($permission_ids=[],$status=0): array{
        $query=StratorPermission::where('pid',0)->whereIn('id',$permission_ids);
        if($status==1){
            $query->where('status',1);
        }
        $stratorMissions=$query->orderBy('sort','asc')->get()->toArray();
        if(count($stratorMissions)){
            foreach ($stratorMissions as $key=>$mission){
                $stratorMissions[$key]['children']=$this->getPermissionsByPid($mission['id'],$permission_ids,$status);
            }
        }
        return $stratorMissions;
    }

    //
    public function getPermissionsByPid($pid,$permission_ids=[],$status=0): array{
        $query=StratorPermission::where('pid',$pid)->whereIn('id',$permission_ids);
        if($status==1){
            $query->where('status',1);
        }
        $children=$query->orderBy('sort','asc')->get()->toArray();
        if(count($children)){
            foreach ($children as $key=>$mission){
                $children[$key]['children']=$this->getPermissionsByPid($mission['id'],$permission_ids,$status);
            }
        }
        return $children;
    }

}
